package cn.edu.neusoft.mall.Dao.impl;

import cn.edu.neusoft.framework.db.JDBCTemplate;
import cn.edu.neusoft.framework.db.RowMapper;
import cn.edu.neusoft.mall.Dao.UserDao;
import cn.edu.neusoft.mall.Dao.adminDao;
import cn.edu.neusoft.mall.entity.Pet;
import cn.edu.neusoft.mall.entity.User;
import cn.edu.neusoft.mall.entity.subscribe;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class adminDaoImpl implements adminDao {
    RowMapper rowMapper=new RowMapper() {
        @Override
        public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
            subscribe Subscribe=new subscribe();
            Subscribe.setUser_id(rs.getInt("s.user_id"));
            Subscribe.setPet_id(rs.getString("s.pet_id"));
            Subscribe.setTime(rs.getTimestamp("s.timestamp"));

            User user = new User();
            user.setId(rs.getInt("u.id"));
            user.setUsername(rs.getString("u.username"));
            user.setPassword(rs.getString("u.password"));
            user.setUserType(rs.getString("u.userType"));
            user.setSex(rs.getString("u.sex"));
            user.setTelephone(rs.getString("u.telephone"));
            user.setName(rs.getString("u.name"));
            user.setPasswordSecret(rs.getString("u.password"));

            Pet pet=new Pet();
            pet.setQueryNumber(rs.getInt("pc.query_number"));
            pet.setName(rs.getNString("pc.name"));
            pet.setBreed(rs.getNString("pc.breed"));
            pet.setGender(rs.getNString("pc.gender"));
            pet.setBirthday(rs.getDate("pc.birthday"));
            pet.setPrice(rs.getDouble("pc.price"));
            pet.setImagePath(rs.getNString("pc.image_path"));
            pet.setImagePath2(rs.getNString("pc.image_path2"));
            pet.setImagePath3(rs.getNString("pc.image_path3"));
            pet.setImagePath4(rs.getNString("pc.image_path4"));
            pet.setSub(String.valueOf(rs.getInt("pc.sub")));
            Subscribe.setPet(pet);
            Subscribe.setUser(user);
            return Subscribe;
        }
    };
    UserDao userDao = new UserDaoImpl();
    @Override
    public List<User> getUserList() {
        return userDao.getUserList();
    }

    @Override
    public List<subscribe> getdogsSubscribeList() {
        String sql = "select * from subscribe s join pet_store.user u on u.id = s.user_id join pet_store.cats pc on s.pet_id = pc.query_number";
        return JDBCTemplate.query(sql, rowMapper,null);
    }
    @Override
    public List<subscribe> getcatsSubscribeList() {
        String sql="select * from subscribe s join pet_store.user u on u.id = s.user_id join pet_store.dogs pc on s.pet_id = pc.query_number";
        return JDBCTemplate.query(sql, rowMapper,null);
    }
}
